// ////////////////////////////////////////////////////////////////////////////////////////
// NAME: Settings.Java 
// AUTHOR: CDT Riley Mckenna 
// LAST MODIFIED: 28 March 2012 
// DESCRIPTION: Contains code for the settings Tab of Nullwall
// LIMITATIONS: 
// CITATIONS: In code
// 
// ////////////////////////////////////////////////////////////////////////////////////////
package prototype.revision;

import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ToggleButton;

public class DetectionSettings extends GenericActivity {

	private ToggleButton wifi;
	private ToggleButton internet;
	private ToggleButton location;
	private ToggleButton network;
	private FileOperations fo = new FileOperations(this);
	private Blocker blocker = new Blocker(fo);
	private PermissionsManager pm = new PermissionsManager();
	private String name = "myPrefs";
	private boolean statusBar = true;
	private GeneralSettings gs = new GeneralSettings();
	SharedPreferences settings;
	SharedPreferences.Editor editor;
	private Activity thisActivity = this;
//private boolean detection = gs.detection;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.detectionsettings);
		builder();
		settings = getSharedPreferences(name, Activity.MODE_PRIVATE);
		editor = settings.edit();
		System.out.println("OnStart");
	}

	@Override
	public void onPause(Bundle savedInstanceState) {
		super.onPause(savedInstanceState);
		saveBool(internet, "internet");
		saveBool(location, "location");
		saveBool(wifi, "wifi");
		saveBool(network, "network");

	}

	@Override
	public void onStop(Bundle savedInstanceState) {
		super.onStop(savedInstanceState);
		saveBool(internet, "internet");
		saveBool(location, "location");
		saveBool(wifi, "wifi");
		saveBool(network, "network");
	}

	@Override
	public void onResume() {
		super.onResume();
		getBool(internet, "internet");
		getBool(location, "location");
		getBool(wifi, "wifi");
		getBool(network, "network");

	}

	public void builder() {
		internet = (ToggleButton) findViewById(R.id.internetToggle);
		network = (ToggleButton) findViewById(R.id.networkToggle);
		wifi = (ToggleButton) findViewById(R.id.wifiToggle);
		location = (ToggleButton) findViewById(R.id.locationToggle);
		// listeners
		wifi.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				if (wifi.isChecked()) {
						// TODO if its checked it will do this...
						blocker.blockall(pm.getACCESS_WIFI_STATE());
					} else {
						// TODO if its not checked it will do this...
						blocker.unblockall(pm.getACCESS_WIFI_STATE());
					}
					saveBool(wifi, "wifi");
				}
		});
		network.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				// TODO Auto-generated method stub
					if (network.isChecked()) {
						// TODO if its checked it will do this...
						blocker.blockall(pm.getACCESS_NETWORK_STATE());
					} else {
						// TODO if its not checked it will do this...
						blocker.unblockall(pm.getACCESS_NETWORK_STATE());
					}
					saveBool(network, "network");
				}
		});
		location.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				// TODO Auto-generated method stub

					if (location.isChecked()) {
						// TODO if its checked it will do this...
						blocker.blockall(pm.getACCESS_FINE_LOCATION());
						blocker.blockall( pm.getACCESS_COUSRE_LOCATION());
					} else {
						// TODO if its not checked it will do this...
						blocker.unblockall( pm.getACCESS_FINE_LOCATION());
						blocker.unblockall( pm.getACCESS_COUSRE_LOCATION());
					}
					saveBool(location, "location");
			}
		});
		internet.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				// TODO Auto-generated method stub
					if (internet.isChecked()) {
						blocker.blockall(pm.getINTERNET());
					} else {
						blocker.unblockall(pm.getINTERNET());
					}
					saveBool(internet, "internet");
			}
		});
	}

	public void getBool(ToggleButton toggle, String name) {
		boolean tg = settings.getBoolean(name, true); // default is true
		if (tg == true) {
			toggle.setChecked(true);
		} else {
			toggle.setChecked(false);
		}
	}

	public void saveBool(ToggleButton toggle, String name) {
		if ((toggle.isChecked())) {
			SharedPreferences.Editor editor = settings.edit();
			editor.putBoolean(name, true); // value to store
			editor.commit();
		} else {
			SharedPreferences.Editor editor = settings.edit();
			editor.putBoolean(name, false); // value to store
			editor.commit();
		}

		;
	}

	public boolean getStatusBarBool() {
		return statusBar;
	}

	public void setStatusBarBool(boolean statusBar) {
		this.statusBar = statusBar;
	}

}
